//	ID : pallab81
// 	PROG : 11504
// 	LANG : C++
// 	2011-07-07-21.44.37 Thursday
//
// 	"I have not failed, I have just found 10000 ways that won't work.
//


#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <fstream>
#include <string>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>

#include <cstdio>
#include <cmath>
#include <cstring>
#include <climits>
#include <cstring>

using namespace std;

#define fo(i,ed) for(int i = 0 ; i < ed ; ++i )
#define foit(i, x) for (typeof((x).begin()) i = (x).begin(); i != (x).end(); i++)
#define bip system("pause")
#define Int long long
#define pb push_back
#define SZ(X) (int)(X).size()
#define LN(X) (int)(X).length()
#define mk make_pair
#define f first
#define s second

int num_node,num_edge;
vector< vector<int> > path;//_path;
int seen[ 10+(int)1e5 ],seen2[10+(int)1e5];
inline void read() {
    scanf("%d %d",&num_node,&num_edge);
    path.assign( num_node+5, vector<int>() );
//    _path.assign( num_node+5, vector<int>() );

    for(int x,y,i=0; i<num_edge; ++i) {
        scanf("%d %d",&x,&y);
        path[x].pb( y );
        //_path[y].pb( x );
    }
}
inline void dfs(int now_node, stack< int > &nodes) {
    if( seen[ now_node ]==-1 ) {
        seen[ now_node ] = 0 ;
        fo(i,SZ(path[now_node])) {
            dfs( path[now_node][i] , nodes);
        }
        nodes.push( now_node );
    }
}
inline void dfs2(int now_node){
    if( seen2[ now_node ]==-1 ){
        seen2[ now_node ] = 0 ;
        fo(i,SZ(path[now_node])){
            dfs2(path[now_node][i]);
        }
    }
}
inline void proc() {
    memset( seen, -1, sizeof(seen) );
    stack<int> nodes;
    for(int i=1; i<=num_node; ++i) {
        if( seen[i]==-1 ) {
            dfs(i, nodes);
        }
    }
    memset( seen2, -1, sizeof(seen2) );
    int tot=0;
    for( int i; nodes.empty()==false ; nodes.pop() ){
            i = nodes.top();
            if( seen2[ i ]==-1){
                dfs2(i);
                ++tot;
            }

    }

    printf("%d\n",tot);
}
int main() {
    int cases;
    for( scanf("%d",&cases); cases-- ; ) {
        read();
        proc();
    }

    return 0;
}

